package cn.itcast.controller;

import cn.itcast.domain.Account;
import cn.itcast.service.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping("/account")
public class AccountController {
@Autowired
private AccountService accountService;
//查询所有数据
    @RequestMapping(value ="/list")
    public String findAll(HttpServletRequest request){
        List<Account> accountList=accountService.findAll();
        request.setAttribute("accountList",accountList);
        return "/list.jsp";
    }
//保存数据
    @Transactional
    @RequestMapping(value ="/save")
    public String save(Account account){
        accountService.save(account);
        return "redirect:/account/list.do";//重定向跳转到list展示页面
    }

    //数据回显
    @RequestMapping(value ="/findById")
    public String findById(Integer id,HttpServletRequest request){
        Account account=accountService.findById(id);
        request.setAttribute("account",account);
        return "/update.jsp";
    }

    //保存数据
    @Transactional
    @RequestMapping(value ="/update")
    public String update(Account account){
        accountService.update(account);
        return "redirect:/account/list.do";//重定向跳转到list展示页面
    }

    //删除数据
    @Transactional
    @RequestMapping(value ="/deleteById")
    public String deleteById(Integer id){
        accountService.deleteById(id);
        return "redirect:/account/list.do";//重定向跳转到list展示页面
    }

}
